package org.hfy.controller;

import org.hfy.common.ApiResponse;
import org.hfy.exception.BusinessException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试异常控制器
 * 
 * @author hfy
 * @version 1.0
 */
@RestController
@RequestMapping("/test/exception")
public class TestExceptionController {

    /**
     * 测试业务异常
     * 
     * @param message 异常消息
     * @return 不会返回，会抛出异常
     */
    @GetMapping("/business")
    public ApiResponse<String> testBusinessException(@RequestParam(defaultValue = "测试业务异常") String message) {
        throw new BusinessException(message);
    }
    
    /**
     * 测试运行时异常
     * 
     * @param message 异常消息
     * @return 不会返回，会抛出异常
     */
    @GetMapping("/runtime")
    public ApiResponse<String> testRuntimeException(@RequestParam(defaultValue = "测试运行时异常") String message) {
        throw new RuntimeException(message);
    }
    
    /**
     * 测试空指针异常
     * 
     * @return 不会返回，会抛出异常
     */
    @GetMapping("/nullpointer")
    public ApiResponse<String> testNullPointerException() {
        String str = null;
        // 故意触发空指针异常
        return ApiResponse.success("字符串长度：" + str.length());
    }
    
    /**
     * 测试算术异常
     * 
     * @return 不会返回，会抛出异常
     */
    @GetMapping("/arithmetic")
    public ApiResponse<String> testArithmeticException() {
        // 故意触发除零异常
        int result = 10 / 0;
        return ApiResponse.success("计算结果：" + result);
    }
} 